<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
      {{dataForm}}
      <el-form-item label="模板名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="模板名称"></el-input>
      </el-form-item>
      <el-form-item label="关联规格" prop="specIds">
        <el-select v-model="dataForm.specIds" multiple placeholder="请选择规格" value-key="id">
          <el-option
            v-for="item in specs"
            :key="item.id"
            :label="item.text"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关联品牌" prop="brandIds">
        <el-select v-model="dataForm.brandIds" multiple placeholder="请选择品牌" value-key="id">
          <el-option
            v-for="item in brands"
            :key="item.id"
            :label="item.text"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="扩展属性" prop="customAttributeItems" >
        <el-button @click="dataForm.customAttributeItems.push({})"  type="success" size="mini" plain>新增扩展属性</el-button>
        <el-table
          :data="dataForm.customAttributeItems"
          style="width: 100%;">
          <el-table-column
            header-align="center"
            align="center"
            label="属性名称">
            <template slot-scope="scope" >
              <el-input v-model="scope.row.text"  placeholder="属性名称"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            header-align="center"
            align="center"
            label="操作">
            <template slot-scope="scope">
              <el-button @click="dataForm.customAttributeItems.splice(scope.$index,1)" type="danger" size="small" plain>删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        name: '',
        specIds: [],
        brandIds: [],
        customAttributeItems: []
      },
      specs:[],       // 所有的规格列表
      brands:[],      // 所有的品牌列表
      dataRule: {
        name: [
          { required: true, message: '模板名称不能为空', trigger: 'blur' }
        ],
        specIds: [
          { required: true, message: '关联规格不能为空', trigger: 'blur' }
        ],
        brandIds: [
          { required: true, message: '关联品牌不能为空', trigger: 'blur' }
        ],
        customAttributeItems: [
          { required: true, message: '自定义属性不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.findBrands();
    this.findSpecs();
  },
  methods: {
    //1. 查询所有品牌列表
    findBrands(){
      this.$http({
        url: this.$http.adornUrl(`/manager/brand/findAll`),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          console.log("brands:",data.list)
          this.brands = data.list.map(m=>{
            return {'id':m.id,'text':m.name}
          })
        }
      })
    },
    //2. 查询所有规格列表
    findSpecs(){
      this.$http({
        url: this.$http.adornUrl(`/manager/specification/findAll`),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          console.log("brands:",data.list)
          this.specs = data.list.map(m=>{
            return {'id':m.id,'text':m.specName}
          })
        }
      })
    },
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {   // 修改模板
          this.$http({
            url: this.$http.adornUrl(`/manager/typetemplate/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.typeTemplate.name
              this.dataForm.specIds = JSON.parse(data.typeTemplate.specIds)
                .map(m=>{
                return {'id':`${m.id}`,'text':m.text}
              })
              this.dataForm.brandIds = JSON.parse(data.typeTemplate.brandIds)
                .map(m=>{
                return {'id':`${m.id}`,'text':m.text}
              })
              this.dataForm.customAttributeItems = JSON.parse(data.typeTemplate.customAttributeItems)
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/manager/typetemplate/${!this.dataForm.id ? 'save' : 'update'}`),
            method: `${!this.dataForm.id ? 'post' : 'put'}`,
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'name': this.dataForm.name,
              'specIds': JSON.stringify(this.dataForm.specIds),
              'brandIds': JSON.stringify(this.dataForm.brandIds),
              'customAttributeItems': JSON.stringify(this.dataForm.customAttributeItems)
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
